import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class PhoneBillCalculator {

    // 计算通话时长（分钟）
    public long calculateCallDuration(String date1, String date2) throws ParseException {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        Date fromDate = simpleFormat.parse(date1);
        Date toDate = simpleFormat.parse(date2);

        Calendar fromCal = Calendar.getInstance();
        fromCal.setTime(fromDate);
        Calendar toCal = Calendar.getInstance();
        toCal.setTime(toDate);

        long phoneTime = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
        long minutes;

        if (phoneTime >= 0 && phoneTime / 1000 < 60 || phoneTime % 60000 > 0) {
            // 不到一分钟按一分钟计算
            minutes = phoneTime / 1000 / 60 + 1;
        } else {
            minutes = phoneTime / 1000 / 60;
        }

        // 处理春季夏令时变更
        if (fromCal.get(Calendar.MONTH) == Calendar.MARCH && fromCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY && fromCal.get(Calendar.HOUR_OF_DAY) == 2) {
            fromCal.add(Calendar.HOUR_OF_DAY, 60); // 将开始时间向前调整一个小时
        }

        // 处理秋季夏令时变更
        if (toCal.get(Calendar.MONTH) == Calendar.NOVEMBER && toCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY && toCal.get(Calendar.HOUR_OF_DAY) == 1) {
            toCal.add(Calendar.HOUR_OF_DAY, 60); // 将结束时间向后调整一个小时
        }

        phoneTime = toCal.getTimeInMillis() - fromCal.getTimeInMillis();

        if (phoneTime >= 0 && phoneTime / 1000 < 60 || phoneTime % 60000 > 0) {
            // 不到一分钟按一分钟计算
            minutes = phoneTime / 1000 / 60 + 1;
        } else {
            minutes = phoneTime / 1000 / 60;
        }

        return minutes;
    }


    // 计算通话费用
    public double calculateCallCost(long durationMinutes) {
        double cost = 0.0;
        if (durationMinutes <= 0) {
            cost = 0.05; // 不足1分钟按1分钟计费
        } else if (durationMinutes <= 20) {
            cost = durationMinutes * 0.05; // 小于等于20分钟，每分钟收费0.05美元
        } else {
            cost = 1.0 + (durationMinutes - 20) * 0.1; // 大于20分钟，收费1.00美元，超过20分钟的部分每分钟收费0.10美元
        }
        return cost;
    }
}
